package com.lyx.question.controller;

import com.lyx.common.result.R;
import com.lyx.question.dto.PaperWitnQuestionDto;
import com.lyx.question.dto.QuestionAnswerDto;
import com.lyx.question.dto.QuestionDto;
import com.lyx.question.dto.RankDto;
import com.lyx.question.entity.Paper;
import com.lyx.question.service.QuestionRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * 做题记录
 * @author 14667
 * @date 2022/7/26 9:29
 */
@RestController
@RequestMapping("/questionRecord")
public class QuestionRecordController {
    @Autowired
    QuestionRecordService questionRecordService;

    /**
     * 首页做题记录展示
     * @param request
     * @return
     */
    @GetMapping("/index")
    public R indexQuestionRecord(HttpServletRequest request){
        List<Paper> data = questionRecordService.indexQuestionRecord(request);
        return R.ok().data("data",data);
    }

    /**
     * 按时间展示所有的做题记录
     * @param request
     * @return
     */
    @GetMapping("/all")
    public R allQuestionRecord(HttpServletRequest request){
        Map<String, List<Paper>> map = questionRecordService.allQuestionRecord(request);
        return R.ok().message("按时间返回做题记录成功").data("data",map);
    }

    /**
     * 首页排名
     * @return
     */
    @GetMapping("/rank")
    public R questionRecordRank(){
        List<RankDto> rankDtos = questionRecordService.questionRecordRank();
        return R.ok().data("data",rankDtos).message("获取排名成功");
    }

    /**
     * 定时保存做题记录到缓存
     * @param paperWitnQuestionDto
     * @param request
     * @return
     */
    @PostMapping("/updateQuestionRecord")
    public R updateQuestionRecord(@RequestBody PaperWitnQuestionDto paperWitnQuestionDto,HttpServletRequest request){
        questionRecordService.updateQuestionRecord(paperWitnQuestionDto,request);
        return R.ok().message("保存成功");
    }

    /**
     * 获取当前试卷的做题记录,用于退出做题界面后返回做题界面
     * @param paperId
     * @param request
     * @return
     */
    @GetMapping("/getCurrentPaperQuestion")
    public R getCurrentPaperQuestion(@RequestParam Integer paperId,HttpServletRequest request){
       PaperWitnQuestionDto questionDtoList = questionRecordService.getCurrentPaperQuestion(paperId,request);
        return R.ok().data("data",questionDtoList).message("获取当前试卷问题记录成功");
    }

    /**
     * 确认作答：这里会返回题目的解析等信息，会生成做题记录，会更新做题排行
     * @param request
     * @param questionId
     * @param userAnswer
     * @return
     */
    @GetMapping("/comfirmAnswerQuestion")
    public R comfirmAnswerQuestion(HttpServletRequest request,@RequestParam Integer questionId,@RequestParam String userAnswer){
        QuestionAnswerDto questionAnswerDto = questionRecordService.comfirmAnswerQuestion(request,questionId,userAnswer);
        return R.ok().data("data",questionAnswerDto).message("获取解析成功");
    }
}
